package eu.reply.android.flashm;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class EventDetailsActivity extends Activity {

	private String location = "";
	Location loc;

	public final static String EXTRA_LOCATION_LATITUDE = "com.reply.android.flashm.EVENT.LOCATION_LATITUDE";
	public final static String EXTRA_LOCATION_LONGITUDE = "com.reply.android.flashm.EVENT.LOCATION_LONGITUDE";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_details);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		((Button) findViewById(R.id.event_details_show_map))
				.setText("Show on Map");

		displayEventDetails(getIntent());
	}

	private void displayEventDetails(Intent pIntent) {
		((TextView) this.findViewById(R.id.event_details_attendees))
				.setText(pIntent
						.getStringExtra(MainActivity.EXTRA_EVENT_ATTENDEES));

		((TextView) this.findViewById(R.id.event_details_date)).setText(pIntent
				.getStringExtra(MainActivity.EXTRA_EVENT_DATE));

		((TextView) this.findViewById(R.id.event_details_description))
				.setText(pIntent
						.getStringExtra(MainActivity.EXTRA_EVENT_DESCRIPTION));

		((TextView) this.findViewById(R.id.event_details_name)).setText(pIntent
				.getStringExtra(MainActivity.EXTRA_EVENT_NAME));

		((ImageView) this.findViewById(R.id.event_details_image))
				.setImageResource(pIntent.getIntExtra(
						MainActivity.EXTRA_EVENT_IMAGE_ID, 0));

		location = pIntent.getStringExtra(MainActivity.EXTRA_EVENT_LOCATION);

		loc = new Location("event location");
		loc.setLatitude(Double.parseDouble(pIntent.getStringExtra(
				MainActivity.EXTRA_EVENT_LOCATION).split(",")[0]));
		loc.setLongitude(Double.parseDouble(pIntent.getStringExtra(
				MainActivity.EXTRA_EVENT_LOCATION).split(",")[1]));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_event_details, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void viewOnMap(View view) {
		Intent intent = new Intent(this, ViewOnMap.class);
		intent.putExtra(MainActivity.EXTRA_EVENT_NAME, getIntent()
				.getStringExtra(MainActivity.EXTRA_EVENT_NAME));
		intent.putExtra(MainActivity.EXTRA_EVENT_DATE, getIntent()
				.getStringExtra(MainActivity.EXTRA_EVENT_DATE));
		intent.putExtra(EXTRA_LOCATION_LATITUDE, loc.getLatitude());
		intent.putExtra(EXTRA_LOCATION_LONGITUDE, loc.getLongitude());
		startActivity(intent);
	}

}
